package com.xmb.wechat.old;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.hacknife.immersive.Immersive;
import com.xmb.wechat.R;
import com.xmb.wechat.adapter.MyFragmentPagerAdapter;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.bean.WechatLastMsgBean;
import com.xmb.wechat.bean.WechatLastMsgBean_;
import com.xmb.wechat.lsn.OnUnReadCountChangeListener;
import com.xmb.wechat.old.find.FindFragment;
import com.xmb.wechat.tab.TabGroupView;
import com.xmb.wechat.tab.TabView;

import java.util.ArrayList;
import java.util.List;

import static com.xmb.wechat.R.id.view_pager;

public class WechatOldMainActivity extends AppCompatActivity implements TabGroupView.OnItemClickListener, OnUnReadCountChangeListener {

    private List<Fragment> mFragments = new ArrayList<>();
    private ViewPager      mViewPager;
    private TabGroupView   mTabGroupView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Immersive.setContentView(this, R.layout.wechat_activity_main, R.color.wechat_status_bar_bg, R.color.black, false, false);
        BarUtils.setStatusBarLightMode(this, true);
        getSupportActionBar().hide();
        initFragments();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        onUnReadCountChange();
    }

    private void changeStatusBarColor(int position) {
        if (position == 3) {
            Immersive.setStatusBarColor(this, getResources().getColor(android.R.color.white));
        } else {
            Immersive.setStatusBarColor(this, getResources().getColor(R.color.wechat_status_bar_bg));
        }
    }

    private void initView() {
        mViewPager = findViewById(view_pager);
        mTabGroupView = findViewById(R.id.group_tab_layout);
        mTabGroupView.setOnItemClickListener(this);
        mTabGroupView.setHasNew(2, true);
        mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        mViewPager.setOffscreenPageLimit(4);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
                mTabGroupView.onScrolling(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                mTabGroupView.setCurrentItem(position);
                mTabGroupView.setHasNew(position, false);
                changeStatusBarColor(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initFragments() {
        mFragments.add(new HomeFragment());
        mFragments.add(new ContactsFragment());
        mFragments.add(new FindFragment());
        mFragments.add(new MineFragment());
    }

    @Override
    public void onClick(TabView tabLayout, int position) {
        mViewPager.setCurrentItem(position, false);
        mTabGroupView.setHasNew(position, false);
    }

    @Override
    public void onUnReadCountChange() {
        int count = (int) WechatStartApplication.getBoxStore().boxFor(WechatLastMsgBean.class).query()
                .build().property(WechatLastMsgBean_.unReadCount).sum();
        LogUtils.d("总未读数量：" + count);
        mTabGroupView.setUnreadCount(0, count);
    }

}
